package com.minelx.table.core;

import com.minelx.table.core.series.Series;
import com.minelx.table.core.series.index.Indexes;
import com.minelx.table.core.series.source.GridColumnSource;
import com.minelx.table.core.series.source.GridRowSource;
import com.minelx.table.core.series.source.GridSource;
import com.minelx.table.core.series.source.OffsetGridSource;

public class GridTableProvider implements TableProvider {
	private final Indexes rowIndexes;

	private final Indexes columnIndexes;

	private final GridSource<String> content;

	private final Series<String> title;

	public GridTableProvider(GridSource<String> gridSource) {
		rowIndexes = Indexes.range(0, gridSource.availableRow() - 1);
		columnIndexes = Indexes.range(0, gridSource.availableColumn());

		content = new OffsetGridSource<>(gridSource, 0, 1);
		title = Series.create(columnIndexes, new GridRowSource<>(gridSource, 0));
	}

	@Override
	public Series<String> title() {
		return title;
	}

	@Override
	public Series<String> row(long at) {
		return Series.create(columnIndexes, new GridRowSource<>(content, at));
	}

	@Override
	public Series<String> column(long at) {
		return Series.create(rowIndexes, new GridColumnSource<>(content, at));
	}

	@Override
	public Indexes rowIndexes() {
		return rowIndexes;
	}

	@Override
	public Indexes columnIndexes() {
		return columnIndexes;
	}
}
